package com.example.asus.zyfypt_tea_307plx.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.example.asus.zyfypt_tea_307plx.R;
import com.example.asus.zyfypt_tea_307plx.bean.LoginBean;
import com.example.asus.zyfypt_tea_307plx.iface.LoginListener;
import com.example.asus.zyfypt_tea_307plx.model.LoginModel;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText etuser, etpass;
    private String username = "", password = "", sessionID = "";
    private Switch sw;
    SharedPreferences sp;
    final static String PREF_NAME="loginPref";//2 定义名字
    SharedPreferences preferences;//2创建变量
    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            sessionID = loginBean.getSessionid();
            System.out.println("----sessionID=" + sessionID);
            if (sessionID != null) {
                Toast.makeText(LoginActivity.this, "登录成功--sessionID=" + sessionID, Toast.LENGTH_SHORT).show();
                SharedPreferences.Editor editor=preferences.edit();//2 savesp
                editor.putString("sessionId", sessionID);//存ID
                editor.putString("userName", etuser.getText().toString());
                editor.putString("password",etpass.getText().toString());
                editor.commit();//确认保存

                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();

            } else
                Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(LoginActivity.this, "登录失败2", Toast.LENGTH_SHORT).show();

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        preferences = getSharedPreferences(PREF_NAME,MODE_PRIVATE);//实例化并规定权限
        String userName=preferences.getString("userName","");//得到用户名
        String password=preferences.getString("password","");
        etuser.setText(userName);//输出用户名
        etpass.setText(password);//输出密码

    }

    private void init() {
        findViewById(R.id.btnlogin).setOnClickListener(this);
        findViewById(R.id.btnregister).setOnClickListener(this);
        etuser = (EditText) findViewById(R.id.editText);
        etpass = (EditText) findViewById(R.id.editText2);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnlogin://登录
                username = etuser.getText().toString();
                password = etpass.getText().toString();
                LoginModel loginModel = new LoginModel();
                loginModel.getLoginResult(username, password, loginListener);

                break;
            case R.id.btnregister://注册
                Toast.makeText(this, "注册", Toast.LENGTH_SHORT).show();
                Toast.makeText(this,"注册",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(this,RegisterActivity.class);
                startActivity(intent);
                finish();
                break;

        }
    }
    private  void saveSP()
    {
        String strname = etuser.getText().toString();
        String strPass = etpass.getText().toString();
        Boolean remember = sw.isChecked();
        SharedPreferences.Editor ed = sp.edit();//定义对象
        ed.putString("sessionId",sessionID);
        ed.putString("userName",strname);
        ed.putString("pass",strPass);
        ed.putBoolean("remember",remember);

        ed.commit();//写到文件里
    }
    private void readSP()
    {
        String strname = sp.getString("userName",null);
        String strpass = sp.getString("pass",null);
        Boolean remember = sp.getBoolean("remember",false);
        if(remember)
        {
            etuser.setText(strname);
            etpass.setText(strpass);
        }
        sw.setChecked(remember);
    }
}



